package com.freesky.biz.abchina.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.freesky.biz.abchina.domain.TRightsOrderRecord;
import com.freesky.biz.abchina.domain.TSpecialGiftRecord;
import com.freesky.biz.abchina.mapper.RightsOrderMapper;
import com.freesky.biz.abchina.mapper.SpecialGiftMapper;
import com.freesky.biz.abchina.service.IRightsOrderService;
import com.freesky.biz.abchina.service.ISpecialGiftService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.common.utils.StringUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecialGiftServiceImpl implements ISpecialGiftService {

    @Autowired
    private SpecialGiftMapper specialGiftMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;

    @Override
    public List<TSpecialGiftRecord> findList(TSpecialGiftRecord entity) {
        List<TSpecialGiftRecord> specialGiftRecordList = specialGiftMapper.findList(entity);
        for(int i=0;i<specialGiftRecordList.size();i++){
            TSpecialGiftRecord specialGiftRecord = specialGiftRecordList.get(i);
            specialGiftRecord.setXml1("0");
            specialGiftRecord.setXml2("0");
            specialGiftRecord.setXml3("0");
            specialGiftRecord.setXml4("0");
            if(StringUtils.isNotEmpty(specialGiftRecord.getRemark())){
                JSONArray jsonArray = JSON.parseArray(specialGiftRecord.getRemark());
                for(int j=0;j<jsonArray.size();j++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(j);
                    String name = jsonObject.getString("name");
                    Integer amount = jsonObject.getInteger("amount");
                    if("XML1".equalsIgnoreCase(name)){
                        specialGiftRecord.setXml1(String.valueOf(amount));
                    } else if("XML2".equalsIgnoreCase(name)){
                        specialGiftRecord.setXml2(String.valueOf(amount));
                    } else if("XML3".equalsIgnoreCase(name)){
                        specialGiftRecord.setXml3(String.valueOf(amount));
                    } else if("XML4".equalsIgnoreCase(name)){
                        specialGiftRecord.setXml4(String.valueOf(amount));
                    }
                }
            }
            specialGiftRecordList.set(i, specialGiftRecord);
        }
        return specialGiftRecordList;
    }

    @Override
    public AjaxResult exportFile(TSpecialGiftRecord entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("SPECIAL_GIFT_RECORD");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TSpecialGiftRecord(), ISpecialGiftService.class,
                "findList", "专项权益赠送平台",Long.parseLong(exportRecord.getId()),sysUser.getUserName());
        return AjaxResult.success();
    }

}
